package com.boomsecret.juc.atomic;

import com.boomsecret.juc.atomic.AtomicIntegerFieldUpdateDemo.LongUser;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author wangjinliang on 2019/1/17.
 */
public class AtomicIntegerThreadDemo {
    private static AtomicIntegerFieldUpdater<LongUser> atomicIntegerFieldUpdate = AtomicIntegerFieldUpdater.newUpdater(LongUser.class, "count");
    private static Thread[] threads = new Thread[5];

    public static void main(String[] args) throws Exception {
        LongUser longUser = new LongUser();
        for (int index = 0; index < 5; index ++) {
            threads[index] = new Thread(()->{
                for (int j = 0; j < 200; j ++) {
                    atomicIntegerFieldUpdate.getAndIncrement(longUser);
                }
            });
            threads[index].start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println(atomicIntegerFieldUpdate.get(longUser));
    }
}